package com.ruoyi.web;

import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.svg2svg.SVGTranscoder;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGSVGElement;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

public class Test2 {

    public static void main(String[] args) {
        try {
            File inputFile = new File("C:\\Users\\86183\\Desktop\\test02.svg");
            File outFile = new File("C:\\Users\\86183\\Desktop\\test02.svg");

            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            String uri = inputFile.toURI().toString();
            SVGDocument svgDocument = f.createSVGDocument(uri);
            SVGSVGElement root = svgDocument.getRootElement();

            NodeList childNodes = root.getChildNodes();
            root.removeChild(childNodes.item(3));
            TranscoderInput input = new TranscoderInput(svgDocument);
            SVGTranscoder t = new SVGTranscoder();
            try (OutputStream os = new FileOutputStream(outFile)) {
                OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                TranscoderOutput output = new TranscoderOutput(writer);
                t.transcode(input, output);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
